package com.baway.biliclitent.fragment.ZoneType;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.RegionPagerAdapter;
import com.baway.biliclitent.custom.NoScrollViewPager;
import com.baway.biliclitent.model.ZoneBean;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2017/3/10.
 */

public class RegionTypeDetailsActivity extends AppCompatActivity {

    private Toolbar toBar;
    private SlidingTabLayout slidingTab;
    private NoScrollViewPager view_pager;
    private List<ZoneBean.DataBeanHeadline.ChildrenBeanHeadline> children;
    private List<String> titles = new ArrayList<>();
    private int index;
    private ZoneBean.DataBeanHeadline bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_region_details);
        //进行接值
        Intent intent = getIntent();
        index = intent.getIntExtra("index", 0);
        bean = (ZoneBean.DataBeanHeadline) intent.getSerializableExtra("bean");
        children = bean.getChildren();
        intiView();
        //初始化适配器
        initViewPager();
    }

    /**
     * 获取id
     */
    private void intiView() {
        toBar = (Toolbar) findViewById(R.id.region_details_toolbar);
        slidingTab = (SlidingTabLayout) findViewById(R.id.region_details_sliding_tabs);
        view_pager = (NoScrollViewPager) findViewById(R.id.region_details_view_pager);
        toBar.setTitle(bean.getName());
        setSupportActionBar(toBar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void initViewPager() {
        //解析传值
        for (int i = 0; i < children.size(); i++) {
            titles.add(children.get(i).getName());
        }

        RegionPagerAdapter mAdapter = new RegionPagerAdapter(
                getSupportFragmentManager(), titles, children);
        view_pager.setOffscreenPageLimit(titles.size());
        view_pager.setAdapter(mAdapter);
        slidingTab.setViewPager(view_pager);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_region, menu);
        return true;
    }
}
